<?php
namespace app\admin\model;
use think\Model;
use think\Session;
use think\Db;
use app\admin\model\Admin as AdminModel;

class Picture extends Model
{
	public function changecate($id,$cateid)
	{
		foreach ($id as $k => $v) {
			$data[] = ['id'=>$v, 'cateid'=>$cateid];
		}
		$picture = new Picture;
		$res = $picture->saveAll($data);
		if ($res) {
			return 1;	//移动成功
		}else{
			return 0;	//移动失败
		}
	}

	public function search($page,$each,$dbtable,$post=0)
	{
		if(request()->isPost()){
			$arr_position[] = $post['ishome'];
			$arr_position[] = $post['isvouch'];
			$arr_position[] = $post['istop'];

			foreach ($arr_position as $k => $v) {
				$position[] = '%'.$v.'%';
			}

			$cateid		= $post['cateid'];
			$data = Db::table('tp_category')->select();
			$allcateid	= new AdminModel();
			$allcateid	= $allcateid->piccateid($data,$cateid);
			$allcateid[] = intval($cateid);

			$keywords	= $post['keywords'];
			Session::set('position',$position,'Category/search');
			Session::set('cateid',$allcateid,'Category/search');
			Session::set('keywords',$keywords,'Category/search');
		}

		$position = Session::get('position','Category/search');
		$cateid = Session::get('cateid','Category/search');
		$keywords = Session::get('keywords','Category/search');

		$table = 'tp_'.$dbtable;

		if ($position) {	//查询数据
			if ($keywords) {
				if ($cateid) {
					$data = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('position','like',$position)
						->where('tp_picture.cateid','in',$cateid)
						->where('titlename','like','%'.$keywords.'%')
						->order('picsort,id')
						->select();
				}else{
					$data = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('position','like',$position)
						->where('titlename','like','%'.$keywords.'%')
						->order('picsort,id')
						->select();
				}	
			}else{
				if ($cateid) {
					$data = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('position','like',$position)
						->where('tp_picture.cateid','in',$cateid)
						->order('picsort,id')
						->select();
				}else{
					$data = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('position','like',$position)
						->order('picsort,id')
						->select();
				}
			}
		}else{
			if ($keywords) {
				if ($cateid) {
					$data = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('titlename','like','%'.$keywords.'%')
						->where('tp_picture.cateid','in',$cateid)
						->order('picsort,id')
						->select();
				}else{
					$data = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('titlename','like','%'.$keywords.'%')
						->order('picsort,id')
						->select();
				}
			}else{
				if ($cateid) {
					$data = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('tp_picture.cateid','in',$cateid)
						->select();
				}else{
					$data = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->select();	
				}
			}
		}

		//$data = db($dbtable)->select();	
		$sum = count($data);	//总条数
		$pagetotal = ceil($sum/$each);	//总页数,尾页
		//限制手动在URL上输入不存在的页面
		if($sum !== 0){
			if($page>$pagetotal || $page<1){
				return -1;
				//$this->error('没有此页面');
			}
		}
		//$list = db($dbtable)->limit(($page-1)*$each,$each)->select();
		//当前页的数据,从第($page-1)*$each条数据起取$each条数据
		if ($position) {	//查询数据
			if ($keywords) {
				if ($cateid) {
					$list = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('position','like',$position)
						->where('tp_picture.cateid','in',$cateid)
						->where('titlename','like','%'.$keywords.'%')
						->limit(($page-1)*$each,$each)
						->order('picsort,id')
						->select();	
				}else{
					$list = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('position','like',$position)
						->where('titlename','like','%'.$keywords.'%')
						->limit(($page-1)*$each,$each)
						->order('picsort,id')
						->select();
				}
			}else{
				if ($cateid) {
					$list = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('position','like',$position)
						->where('tp_picture.cateid','in',$cateid)
						->limit(($page-1)*$each,$each)
						->order('picsort,id')
						->select();
				}else{
					$list = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('position','like',$position)
						->limit(($page-1)*$each,$each)
						->order('picsort,id')
						->select();
				}
			}
		}else{
			if ($keywords) {
				if ($cateid) {
					$list = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('titlename','like','%'.$keywords.'%')
						->where('tp_picture.cateid','in',$cateid)
						->limit(($page-1)*$each,$each)
						->order('picsort,id')
						->select();
				}else{
					$list = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('titlename','like','%'.$keywords.'%')
						->limit(($page-1)*$each,$each)
						->order('picsort,id')
						->select();
				}
			}else{
				if ($cateid) {
					$list = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->where('tp_picture.cateid','in',$cateid)
						->limit(($page-1)*$each,$each)
						->order('picsort,id')
						->select();	
				}else{
					$list = Db::table($table)
						->alias('pic')
						->join('category cate','pic.cateid = cate.cateid')
						->limit(($page-1)*$each,$each)
						->order('picsort,id')
						->select();	
				}
			}
		}

		$res['list'] = $list;
		$res['pagetotal'] = $pagetotal;
		$res['page'] = $page;
		return $res;
	}

	public function delpic($id)
	{
		if (is_array($id)) {
            foreach ($id as $k => $v) {
                 $data = db('picture')->where('id',$v)->find();
                // 删除原来的小图片
                $smallpic=$_SERVER['DOCUMENT_ROOT'].$data['smallpic'];
                if(file_exists($smallpic)){
                    @unlink($smallpic);
                }
                $bigpic = explode('|', $data['bigpic']);
                foreach ($bigpic as $k => $v) {
                    //删除原来的大图片
                    $bigpic=$_SERVER['DOCUMENT_ROOT'].$v;
                    if(file_exists($bigpic)){
                        @unlink($bigpic);
                    }
                }
                
             } 

        }else{
            $data = db('picture')->where('id',$id)->find();
            //删除原来的小图片
            $smallpic=$_SERVER['DOCUMENT_ROOT'].$data['smallpic'];
            if(file_exists($smallpic)){
                @unlink($smallpic);
            }
            $bigpic = explode('|', $data['bigpic']);
                foreach ($bigpic as $k => $v) {
                    //删除原来的大图片
                    $bigpic=$_SERVER['DOCUMENT_ROOT'].$v;
                    if(file_exists($bigpic)){
                        @unlink($bigpic);
                    }
                }
        }

        $res = db('picture')->delete($id);
        return $res;
	}

}